package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"sync"
	"zg2zy4/goods_srv/app_config"
)

var (
	Db   *gorm.DB
	err  error
	once sync.Once
)

func InitMysql() {
	appConf := app_config.AppConf.MySQLConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		appConf.User, appConf.Pwd, appConf.Host, appConf.Port, appConf.Database)
	once.Do(func() {
		Db, err = gorm.Open(mysql.Open(dsn))
	})
	if err != nil {
		fmt.Println("数据库连接失败", err)
		return
	}
	Db.AutoMigrate(new(Goods))

	fmt.Println("数据库连接成功")
}
